<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Store_Model extends CI_Model
{

	function get_featured_product(){

		//$this->db->where('product_id', $product_id);
		$this->db->limit(12);
		$this->db->where('status',1);
		$this->db->order_by('product_id DESC');

		$query = $this->db->get('product');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();		

	}
	
	function select($product_seo_name){
        $query = $this->db->get_where('product', array('product_seo_name'=>$product_seo_name));
		if($query ->num_rows() >0 ) {
			foreach($query ->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
    }
	
	function get_store($store=''){
		$this->db->where('toko_id', $store);

		$query = $this->db->get('store');
		if ($query->num_rows() > 0) return $query->result();
		return NULL;
	}

	function get_list_product($store='', $page='', $limit=''){
		
		$query = $this->db->query('SELECT * FROM product WHERE user_id in (select user_id from store where toko_id="'.$store.'")order by date_add desc');
		if(!empty($limit))
		$query = $this->db->query('SELECT * FROM product WHERE user_id in (select user_id from store where toko_id="'.$store.'")order by date_add desc limit '.$page.','.$limit.'');
		
		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function get_list_product_low($store='', $page='', $limit=''){
		
		$query = $this->db->query('SELECT * FROM product WHERE user_id in (select user_id from store where toko_id="'.$store.'")order by price asc');
		if(!empty($limit))
		$query = $this->db->query('SELECT * FROM product WHERE user_id in (select user_id from store where toko_id="'.$store.'")order by price asc limit '.$page.','.$limit.'');
		
		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function get_list_product_high($store='', $page='', $limit=''){
		
		$query = $this->db->query('SELECT * FROM product WHERE user_id in (select user_id from store where toko_id="'.$store.'")order by price desc');
		if(!empty($limit))
		$query = $this->db->query('SELECT * FROM product WHERE user_id in (select user_id from store where toko_id="'.$store.'")order by price desc limit '.$page.','.$limit.'');
		
		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	
	function get_category_parent(){
		$this->db->where('parent', 0);

		$query = $this->db->get('product_category');
		if ($query->num_rows() > 0) return $query->result();
		return NULL;
	}
	
	function get_category(){
		$query = $this->db->get('product_category');
		if ($query->num_rows() > 0) return $query->result();
		return NULL;
	}

	function get_category_child($parent){
		$this->db->where('parent', $parent);

		$query = $this->db->get('product_category');
		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function create(){
		$data = array(
					'user_id' => Modules::run('auth/get_user_id'),
					'nama_toko' =>  $this->input->post('nama_toko'),
					'judul_toko' => $this->input->post('nama_toko'),
					'date_add' => date ('Y-m-d H:i:s')
				);
		$this->db->insert('store', $data);	
	}
	
}